//
//  WmcScreenTextWatermarkView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/8.
//

import UIKit

class WmcScreenTextWatermarkView: WmcBaseView {
    // Lifecycle

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    convenience init(frame: CGRect = Wmc_screen,
                     text: String,
                     color: UIColor,
                     font: UIFont,
                     alignment: NSTextAlignment = .center,
                     rotationAngle: CGFloat = -45,
                     spacing: CGFloat = 10)
    {
        self.init(frame: frame)
        self.text = text
        self.color = color
        self.font = font
        self.alignment = alignment
        self.rotationAngle = rotationAngle
        self.spacing = spacing
        self.clipsToBounds = true
        self.backgroundColor = .clear
        self.drawTextLayer()
    }

    // Internal

    var text: String = ""
    var color: UIColor = .gray
    var font: UIFont = 12.yp.uiFont
    var alignment: NSTextAlignment = .center
    var rotationAngle: CGFloat = -45
    var spacing: CGFloat = 0

    var layers: [CATextLayer] = []

    /// 计算文字大小
    func textSize(text: String, font: UIFont) -> CGSize {
        let maxSize = CGSize(width: CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT))
        let textSize = text.boundingRect(with: maxSize,
                                         options: .usesLineFragmentOrigin,
                                         attributes: [.font: font],
                                         context: nil)
        return CGSize(width: textSize.width + 1,
                      height: textSize.height + 1)
    }

    /// 设置文字
    func setText(_ text: String) {
        self.text = text
        self.drawTextLayer()
    }

    /// 设置文字颜色
    func setTextColor(_ color: UIColor) {
        layers.forEach { textLayer in
            textLayer.foregroundColor = color.cgColor
        }
    }

    /// 绘制layer
    func drawTextLayer() {
        // 移除layer
        layers.forEach { layer in
            layer.removeFromSuperlayer()
        }
        // 计算文字大小
        let textSize = textSize(text: text, font: font)
        // 角度
        let radian = rotationAngle.yp.angleToRadian
        // 求旋转后的实际Y轴垂直偏移量
        let x_sinValue = abs(sin(radian))
        let x_offsetWidth = x_sinValue * textSize.width
        let x_offsetHeight = x_sinValue * textSize.height
        let y_sp = abs(tan(radian)) * spacing
        let offsetY = x_offsetWidth + x_offsetHeight + y_sp
        // 求旋转后的实际X轴水平偏移量
        let y_sinValue = abs(sin((90 - rotationAngle).yp.angleToRadian))
        let y_offsetWidth = y_sinValue * textSize.width
        let y_offsetHeight = y_sinValue * textSize.height
        let offsetX = y_offsetWidth + y_offsetHeight + spacing

        // 计算宽度需要多少个
        let max = max(frame.size.width, frame.size.height)
        let row = ceil(max / offsetX).yp.int + 1
        let column = ceil(max / offsetY).yp.int + 1

        // 事视图整体居中的偏移量
        let screenOffsetX = (frame.size.width - (offsetX * row.yp.cgFloat)) / 2
        let screenOffsetY = (frame.size.height - (offsetY * column.yp.cgFloat)) / 2

        for i in 0 ..< column {
            // y轴
            let y = i.yp.cgFloat * offsetY + screenOffsetY
            for j in 0 ..< row {
                // x轴
                let x = j.yp.cgFloat * offsetX + screenOffsetX
                let textLayer = CATextLayer()
                textLayer.frame = CGRect(origin: CGPoint(x: x, y: y), size: textSize)
                textLayer.string = text
                textLayer.font = font.fontName.yp.cfString
                textLayer.fontSize = font.pointSize
                textLayer.foregroundColor = color.cgColor
                textLayer.alignmentMode = alignment.yp.caTextLayerAlignmentMode!
                textLayer.transform = CATransform3DMakeRotation(radian, 0, 0, 1)
                textLayer.contentsScale = UIScreen.main.scale
                textLayer.isWrapped = true
                layer.addSublayer(textLayer)
                layers.append(textLayer)
            }
        }
    }
}
